﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DLL;
using CommonHelpers;
using Printing;
using System.Configuration;
using Logging;

namespace QuanLyBanLe
{
    public partial class FormBillHistoryDetail : Form
    {
        private int m_BillId;
        private Bill m_Bill;
        private BillService m_BillService;
        private BillDetailService m_BillDetailService;
        private PrintDGV printing;

        public FormBillHistoryDetail(int billId)
        {
            InitializeComponent();
            m_BillId = billId;
            m_BillService = new BillService();
            m_BillDetailService = new BillDetailService();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Print();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormBillHistoryDetail_Load(object sender, EventArgs e)
        {
            m_Bill = m_BillService.GetById(m_BillId);
            lblCustomerName.Text = m_Bill.CustomerName;
            lblSaleDate.Text = m_Bill.SaleTime.ToString("dd/MM/yyyy hh:mm");
            lblBillId.Text = m_Bill.Id.ToString();
            lblBillIndex.Text = m_Bill.DayIndex.ToString();
            lblPhoneNumber.Text = m_Bill.CustomerPhone.ToString();
            lblAddress.Text = m_Bill.CustomerAddress;

            dtpBillDetail.AutoGenerateColumns = false;
            List<pGetBIllDetailByBillId_Result> billDetails = m_BillDetailService.GetBillDetailByBillId(m_BillId);
            dtpBillDetail.DataSource = billDetails;
            int totalPrice = 0;
            foreach (var billDetail in billDetails)
            {
                totalPrice += billDetail.TotalPrice.Value;
            }
            lblTotalPrice.Text = totalPrice.ToMoney();
            SetUpPrint();
            this.Text += FormMain.GetShopInfo();
        }

        private void SetUpPrint()
        {
            List<string> printColumns = new List<string>();
            printColumns.Add("colProduct");
            printColumns.Add("colAmount");
            printColumns.Add("colTotalPrice");
            printColumns.Add("colWarrantyTime");

            List<float> columnsWidth = new List<float>();
            columnsWidth.Add(150);
            columnsWidth.Add(20);
            columnsWidth.Add(80);
            columnsWidth.Add(25);
            string printerName = ConfigurationManager.AppSettings["BillPrinterName"];
            string title = FormMain.s_Setting.ShopNamePrefix + " " + FormMain.s_Setting.ShopName;
            printing = new PrintDGV(dtpBillDetail, printColumns, columnsWidth, title, printerName, true);
        }

        private void SetUpPrintBillHeaderAndFooter()
        {
            //Headers
            PrintText printText;
            List<PrintText> headerRegionTexts = new List<PrintText>();
            if (FormMain.s_Setting != null)
            {
                string namePrefix = FormMain.s_Setting.ShopNamePrefix;
                string name = FormMain.s_Setting.ShopName;
                string address = FormMain.s_Setting.ShopAddress;
                string phoneNumber = FormMain.s_Setting.ShopPhoneNumber;

                printText = new PrintText(namePrefix, new Font("Arial", 9), StringAlignment.Near);
                headerRegionTexts.Add(printText);

                printText = new PrintText(name, new Font("Arial", 20, FontStyle.Italic | FontStyle.Bold), StringAlignment.Center);
                headerRegionTexts.Add(printText);

                printText = new PrintText("ĐC: " + address, new Font("Arial", 9), StringAlignment.Near);
                headerRegionTexts.Add(printText);

                printText = new PrintText("ĐT: " + phoneNumber, new Font("Arial", 9), StringAlignment.Near);
                headerRegionTexts.Add(printText);
            }

            string tmpText;
            tmpText = "Hóa đơn " + m_Bill.Id.ToString() + " - " +
                        m_Bill.SaleTime.ToString("dd/MM/yyyy hh:mm");
            printText = new PrintText(tmpText);
            headerRegionTexts.Add(printText);

            //printText = new PrintText("Khách hàng: " + m_currentBill.CustomerName, new Font("Arial", 9), StringAlignment.Near);
            //headerRegionTexts.Add(printText);

            //if (!string.IsNullOrEmpty(m_currentBill.CustomerAddress.ToString()))
            //{
            //    printText = new PrintText("ĐC: " + m_currentBill.CustomerAddress, new Font("Arial", 9), StringAlignment.Near);
            //    headerRegionTexts.Add(printText);
            //}

            //if (m_currentBill.CustomerPhone != null && !string.IsNullOrEmpty(m_currentBill.CustomerPhone.ToString()))
            //{
            //    printText = new PrintText("ĐT: " + m_currentBill.CustomerPhone, new Font("Arial", 9), StringAlignment.Near);
            //    headerRegionTexts.Add(printText);
            //}

            printing.headerRegionTexts = headerRegionTexts;

            //Sum Region
            List<PrintText> sumRegionTexts = new List<PrintText>();
            printText = new PrintText("Tổng cộng: " + lblTotalPrice.Text, new Font("Arial", 10, FontStyle.Bold), StringAlignment.Far);
            sumRegionTexts.Add(printText);
            printing.sumRegionTexts = sumRegionTexts;

            //Footer
            List<PrintText> footerRegionTexts = new List<PrintText>();
            printText = new PrintText("********************", new Font("Arial", 9, FontStyle.Bold), StringAlignment.Center);
            footerRegionTexts.Add(printText);

            printText = new PrintText("Quý khách vui lòng giữ lại hóa đơn để được", new Font("Arial", 9, FontStyle.Bold | FontStyle.Italic), StringAlignment.Near);
            footerRegionTexts.Add(printText);

            printText = new PrintText("bảo hành", new Font("Arial", 9, FontStyle.Bold | FontStyle.Italic), StringAlignment.Near);
            footerRegionTexts.Add(printText);

            printText = new PrintText("\n", new Font("Arial", 9, FontStyle.Bold | FontStyle.Italic), StringAlignment.Near);
            footerRegionTexts.Add(printText);

            printText = new PrintText("Cảm ơn quý khách", new Font("Arial", 9, FontStyle.Bold | FontStyle.Italic), StringAlignment.Center);
            footerRegionTexts.Add(printText);

            printText = new PrintText("Hẹn gặp lại", new Font("Arial", 9, FontStyle.Bold | FontStyle.Italic), StringAlignment.Center);
            footerRegionTexts.Add(printText);

            printing.footerRegionTexts = footerRegionTexts;
        }

        private void Print()
        {
            try
            {
                SetUpPrintBillHeaderAndFooter();
                printing.Print();
            }
            catch (Exception ex)
            {
                MyLogger.LogException("Lỗi xảy ra khi in", ex);
                MessageBox.Show("Có lỗi xảy ra: " + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
